<?php

require_once APP_PATH.'/Controller/BaseController.php';
require_once APP_PATH.'/Model/CategoryModel.php';
require_once APP_PATH.'/Model/ArticleModel.php';

class ArticleController extends BaseController {
	
	public function index(){
		$categoryModel = new CategoryModel();
		$catelist = $categoryModel->getScrollData();
		
		$articleModel = new ArticleModel();
		$articlelist = $articleModel->getScrollData();
		
		$this->assign('catelist',$catelist);
		$this->assign('articlelist',$articlelist);
		$this->display('Article/article_list.html');
	}
	
	public function add(){
		if (isset($_POST['s'])&&$_POST['s']=='add'){
			$article = array();
			$article['title'] = $_POST['title'];
			$article['cateid'] = $_POST['cateid'];
			$article['author'] = $_POST['author'];
			$article['is_private'] = $_POST['is_private'];
			$article['is_self'] = $_POST['is_self'];
			$article['is_commend'] = $_POST['is_commend'];
			$article['is_index'] = $_POST['is_index'];
			$article['is_caogao'] = 0;
			$article['content'] = stripslashes($_POST['content']);
			$article['summary'] = stripslashes($_POST['summary']);
			$article['page_title'] = $_POST['title'];
			$article['page_keywords'] = $_POST['title'];
			$article['page_description'] = $_POST['title'];
			$article['htmlpath'] = date('Ymd').rand(1000, 9999);
			$article['create_time'] = date('Y-m-d H:i:s');
			$article['update_time'] = date('Y-m-d H:i:s');
			$articleModel = new ArticleModel();
			$articleModel->insert($article);
			exit();
		}
		$categoryModel = new CategoryModel();
		$catelist = $categoryModel->getScrollData();
		
		$this->assign('categorylist',$catelist);
		$this->display('Article/article_add.html');
	}
	
	public function edit(){
		$articleModel = new ArticleModel();
		
		if (isset($_POST['s'])&&$_POST['s']=='edit'){
			$article = array();
			$article['art_id'] = $_POST['artid'];
			$article['title'] = $_POST['title'];
			$article['cateid'] = $_POST['cateid'];
			$article['author'] = $_POST['author'];
			$article['is_private'] = $_POST['is_private'];
			$article['is_self'] = $_POST['is_self'];
			$article['is_commend'] = $_POST['is_commend'];
			$article['is_index'] = $_POST['is_index'];
			$article['is_caogao'] = 0;
			$article['content'] = stripslashes($_POST['content']);
			$article['summary'] = stripslashes($_POST['summary']);
			$article['page_title'] = $_POST['title'];
			$article['page_keywords'] = $_POST['title'];
			$article['page_description'] = $_POST['title'];
			$article['htmlpath'] = $_POST['htmlpath'];
			$article['update_time'] = date('Y-m-d H:i:s');
			$articleModel = new ArticleModel();
			$articleModel->update($article);
			exit();
		}
		
		$art_id = $_GET['artid'];
		$article = $articleModel->select($art_id);
		
		$categoryModel = new CategoryModel();
		$catelist = $categoryModel->getScrollData();
		
		$this->assign('categorylist',$catelist);
		$this->assign('article',$article);
		$this->display('Article/article_edit.html');
	}
}